﻿#region Header

/*
Behavioral Rating of Dancing Human Crowds based on Motion Patterns
By

Pascal Hauser 
Dipl. Ing. in Informatik, Hochschule für Technik Rapperswil, 2006
Master Thesis, Hochschule für Technik Rapperswil, 2008-2010

and

Raphael Gfeller
Dipl. Ing. in Informatik, Hochschule für Technik Rapperswil, 2006
Master Thesis, Hochschule für Technik Rapperswil, 2008-2010

*/

#endregion

#region Usings

using System;
using Emgu.CV;
using Emgu.CV.Structure;
using paravili.Services;
using Sebarf.Services.Interfaces;

#endregion

namespace paravili.Steps {
	/// <summary>
	/// Calculates the difference between to images
	/// </summary>
	public class CalculateDifferencesFromImages : ProcessStepWithMeasurement<Image<Lab, Byte>> {
		#region Public Properties

		/// <summary>
		/// remember the last image
		/// </summary>
		public Image<Lab, Byte> LastImage { get; private set; }

		[ServiceRequest]
		public IResetHandlerService ResetHandlerService {
			set { value.Reset += OnReset; }
		}

		[ServiceRequest(IsOptional = true)]
		public IImageProviderService ImageProviderService { get; set; }

		[ConfigurabelStringValue(Name = "ImageProviderId")]
		public String ImageProviderId { get; set; }

		[ConfigurabelBooleanValue(Name = "ForwardImageToImageProvider")]
		public bool ForwardImageToImageProvider { get; set; }

		#endregion

		#region Public Methods

		public CalculateDifferencesFromImages() {
			ImageProviderId = "CalculateDifferencesFromImages";
			ForwardImageToImageProvider = false;
		}

		protected override Image<Lab, byte> OnProcess(Image<Lab, byte> toProcess) {
			if (LastImage != null && LastImage.Size != toProcess.Size) {
				LastImage = null;
			}
			if (LastImage == null) {
				LastImage = toProcess;
				return null;
			}
			else {
				Image<Lab, byte> toReturn = toProcess - LastImage;
				LastImage = toProcess;
				if (ForwardImageToImageProvider) {
					ImageProviderService.SetImage(ImageProviderId, toReturn);
				}
				return LastImage;
			}
		}

		#endregion

		#region Private Methods

		private void OnReset(Object sender, EventArgs args) {
			LastImage = null;
		}

		#endregion
	}
}